package com.tang.crm.controller;

import com.tang.crm.annotation.RequirePermission;
import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.entity.SaleChance;
import com.tang.crm.dto.SaleChanceDto;
import com.tang.crm.service.SaleChanceService;
import com.tang.crm.utils.CookieUtil;
import com.tang.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 营销机会
 * @author SHIRELY阳
 */
@Controller
@RequestMapping("/sale_chance")
public class SaleChanceController extends BaseController {

    @Autowired
    private SaleChanceService saleChanceService;

    /**
     * 进入营销机会管理页面
     * @return
     */
    @RequirePermission(code = "1010")
    @RequestMapping("/index")
    public String index(){
        return "saleChance/sale_chance";
    }

    /**
     * 进入到更新/修改营销机会管理的页面
     * @return
     */
    @RequestMapping("/toSaleChancePage")
    public String toSaleChancePage(Integer saleChanceId, Model model){
        // 当执行更新操作时，判断id是否为空
        if (saleChanceId != null){
            // 查到该数据，进行页面数据的回响
            model.addAttribute("saleChance", saleChanceService.getById(saleChanceId));
        }
        return "saleChance/add_update";
    }

    /**
     * 遍历营销机会管理数据
     * @param saleChanceDto
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Map<String, Object> querySaleChanceByParams(Integer flag, HttpServletRequest request,
                                                       SaleChanceDto saleChanceDto){
        // 判断flag的值
        if (flag != null && flag == 1){
            // 说明是查询客户开发计划
            int id = LoginUserUtil.releaseUserIdFromCookie(request);
            // 设置指派人（当前登录用户的ID）
            // 从cookie中获取当前登录用户的ID
            saleChanceDto.setAssignMan(id);
        }
        return saleChanceService.querySaleChanceByParams(saleChanceDto);
    }

    /**
     * 添加营销机会管理
     * @param request
     * @param saleChance
     * @return
     */
    @RequirePermission(code = "101002")
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo insertSaleChance(HttpServletRequest request, SaleChance saleChance){
        // 从cookie中获取userName
        String userName = CookieUtil.getCookieValue(request, "userName");
        // 设置创建人
        saleChance.setCreateMan(userName);
        saleChanceService.saveSaleChance(saleChance);
        return success("营销机会数据添加成功");
    }

    /**
     * 更新营销机会数据
     * @param saleChance
     * @return
     */
    @RequirePermission(code = "101004")
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){
        saleChanceService.updateSaleChance(saleChance);
        return success("营销机会数据更新成功");
    }

    /**
     * 删除营销机会
     * @param saleChanceIds
     * @return
     */
    @RequirePermission(code = "101003")
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteSaleChances(@RequestParam("ids") List<Integer> saleChanceIds){
        saleChanceService.deleteSaleChances(saleChanceIds);
        return success("营销机会数据删除成功");
    }

    /**
     * 更新营销机会开发状态
     * @param id
     * @param devResult
     * @return
     */
    @PostMapping("/updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChanceDevResult(Integer id, Integer devResult){
        saleChanceService.updateSaleChanceDevResult(id, devResult);
        return success("更新开发状态成功");
    }
}
